<?php require('header.php'); ?>

<?php 
// 540400
// 	printf("<div class='nyi'>-->%s<--</div>", );

// query string
$q="";
$begins = "%";
$ends = "%";

if (isset($_GET['query'])) {
	$searchString = mysql_real_escape_string($_GET['query']);
} else {
	$searchString = mysql_real_escape_string($_POST['searchString']);	
}

if (($_GET['2D'] == 1)|| (isset($POST['SHOW2D']))) {
	$show_2D = 1; 
} else {
	$show_2D = 0;
}

$range = array( 
	mysql_real_escape_string($_POST['HBDONOR_']),
	mysql_real_escape_string($_POST['HBDONOR']),
	mysql_real_escape_string($_POST['HBACCEPTOR_']),
	mysql_real_escape_string($_POST['HBACCEPTOR']),
	mysql_real_escape_string($_POST['MW_']),
	mysql_real_escape_string($_POST['MW']),
	mysql_real_escape_string($_POST['LOGP_']),
	mysql_real_escape_string($_POST['LOGP']),
	mysql_real_escape_string($_POST['RBONDS_']),
	mysql_real_escape_string($_POST['RBONDS'])
	);

$rowsPerPage = 15;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page'])) {
	$page = $_GET['page'];
} else {
	$page = 0;
}

$offset = $page;

?>

<form name='Search' method='post' action=<?php echo "'". $_SERVER['PHP_SELF'] ."'"; ?>>
	<div id="left-nav">
		<div class="inner-frame">
			<?php require('search_params.php'); ?>
		</div> <!-- end inner-frame -->
	</div> <!-- end left-nav -->

	<div id="content">
		<div class="inner-frame">
			<blockquote>
				<center>
					<a href=<?php printf("'%s'", home_page()); ?>>
						<img src='images/logo.png'><br/>
					</a>
					<input type='text' name='searchString' size='50' maxsize='255' value=<?php echo "'". $searchString ."'"; ?> /><br/><br/>
					<button type="submit" name='edit' onclick="javascript:document.Search.submit();">BEGIN SEARCH</button>
				</center>

				<br/><hr/>
				<?php // display_results($searchString, $rowsPerPage, $page ); ?>
				<table width='100%'> <!-- begin results -->
					<tr>
						<th colspan='3'>
							<code><i><?php echo date('l F jS\, Y h:i:s A'); ?> UTC</i><br/>
								[<a href=''>Save These Results</a>]</code><br/>
							</th>
						</tr>
						<tr><td colspan="3" align="center">
							<small>

								<?php 
								$count = $page+1;
								$prev = ($page <= $rowsPerPage) ? 0 : ($page - $rowsPerPage);
								$next = $page + $rowsPerPage;
							// result page navigation
							printf('<a href="%s?page=%d&2D=%s&query=%s">&lt;&lt;[PREV]</a>', $_PHP['SELF'], $prev, $show_2D, $searchString);
							echo '&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;';
							printf('<a href="%s?page=%d%s&query=%s">[NEXT]&gt;&gt;</a>', $_PHP['SELF'], $next, (isset($show_2D) ? "&2D=1" : "&2D=0") , $searchString);
							?>

						</small><br/>
						</td></tr>
						<?php 
					
					// set comparison location of string
					if (isset($_POST['BEGINS'])) { $begins = ""; }
					if (isset($_POST['ENDS'])) { $ends = ""; }

					/// for timing --------
					$time = microtime(); 
					$time = explode(" ", $time); 
					$time = $time[1] + $time[0]; 
					$time1 = $time;
					///-------------------
					if (is_numeric($searchString)) {
						$q = "SELECT * FROM Compounds WHERE PUBCHEM_COMPOUND_CID = " . $searchString . ";"; 
						$result = mysql_query($q);
						echo "<tr><td style='border: 1px solid black;'>\n<code>\n";
						while ($data = mysql_fetch_assoc($result)) {
							printf("<b>CID:</b> %s<br/>\n", $data['PUBCHEM_COMPOUND_CID']);
							printf("<b>SMILES:</b> %s<br/>\n", $data['PUBCHEM_OPENEYE_ISO_SMILES']);
							printf("<b>mol:</b> %s<br/>\n", $data['mol']);
							printf("<b>Name:</b> %s<br/>\n", $data['PUBCHEM_IUPAC_NAME']);
							printf("<b>Molecular Weight:</b> %s<br/>\n", $data['PUBCHEM_MOLECULAR_WEIGHT']);
							printf("<b>H Bond Donors:</b> %s<br/>\n", $data['PUBCHEM_CACTVS_HBOND_DONOR']);
							printf("<b>H Bond Acceptors:</b> %s<br/>\n", $data['PUBCHEM_CACTVS_HBOND_ACCEPTOR']);
							printf("<b>Log(P):</b> %s<br/>\n", $data['PUBCHEM_XLOGP3_AA']);
							echo "</code></td>";
							echo "<td align='center' style='border: 1px solid black;'>";
							show_2d_smiles_structure( $data['PUBCHEM_OPENEYE_ISO_SMILES'] );
							echo "</td>";
							echo"</tr><tr><td colspan='2' style='border: 1px solid black;><div style='height:300px; overflow:auto;'><code>";
							// generate mol2 file
							echo preg_replace('/\n/', "<br/>", generate_mol2($searchString));
							echo "</code></div></td></tr>";
						}
					} 
					else if ($searchString !== "") {
						if (isset($_POST['SMILES'])) {
							$rq = generate_range_query($range);
							// remove AND if there are no range params
							if ($rq !== "") {
								$q = "SELECT * FROM Compounds WHERE " . $rq . " AND PUBCHEM_OPENEYE_ISO_SMILES LIKE '" . $begins . $searchString . $ends . "' LIMIT ".$offset.", ".$rowsPerPage.";";
								} else {
									$q = "SELECT * FROM Compounds WHERE PUBCHEM_OPENEYE_ISO_SMILES LIKE '" . $begins . $searchString . $ends . "' LIMIT ".$offset.", ".$rowsPerPage.";";
									}

									$result = mysql_query($q);

									if ($result) { 
										while ($data = mysql_fetch_assoc($result)) {
											printf("<td valign='top' align='left' style='border-bottom: 1px dashed black;'><code>%d.</code></td>", $count++);
											printf("<td style='border-bottom: 1px dashed black;'>\n<code>");
											printf("<b>CID: </b><a href='".$_PHP['SELF']."?query=%s'>%s</a><br/>\n", $data['PUBCHEM_COMPOUND_CID'], $data['PUBCHEM_COMPOUND_CID']);
											printf("<b>SMILES:</b> %s<br/>\n", $data['PUBCHEM_OPENEYE_ISO_SMILES']);
											printf("<b>Mol:</b> %s<br/>\n", $data['mol']);
											printf("<div style='overflow:auto;'><code><b>Name:</b> %s</code></div><br/>\n", $data['PUBCHEM_IUPAC_NAME']);
											echo "</code>\n</td>\n";
											echo "<td style='border-bottom: 1px dashed black;'>";
											show_2d_smiles_structure( $data['PUBCHEM_OPENEYE_ISO_SMILES'] );
											echo "</td>";
											echo '</tr>';
										}
									} else {
										echo "\n<td>No Smiles Results</td></tr>\n";
									}
								} 
								else {
									$rq = generate_range_query($range);
									// remove AND if there are no range params
									if ($rq !== "") {
										$q = "SELECT * FROM Compounds WHERE " . $rq . " AND pubchem_iupac_name LIKE '" . $begins . $searchString . $ends . "' LIMIT ".$offset.", ".$rowsPerPage.";";
									} else {
										$q = "SELECT * FROM Compounds WHERE pubchem_iupac_name LIKE '" . $begins . $searchString . $ends . "' LIMIT ".$offset.", ".$rowsPerPage.";";
									}

									$result = mysql_query($q);
									if ($result) { 
										while ($data = mysql_fetch_assoc($result)) {
											printf("<td valign='top' align='left' style='border-bottom: 1px dashed black;'><code>%d.</code></td>", $count++);
											printf("<td style='border-bottom: 1px dashed black;'>\n<code>");
											printf("<b>CID:</b> <a href='".$_PHP['SELF']."?query=%s'>%s</a><br/>\n", $data['PUBCHEM_COMPOUND_CID'], $data['PUBCHEM_COMPOUND_CID']);
											printf("<b>SMILES:</b> %s<br/>\n", $data['PUBCHEM_OPENEYE_ISO_SMILES']);
											printf("<b>Mol:</b> %s<br/>\n", $data['mol']);
											printf("<b>Name:</b> %s<br/>\n", $data['PUBCHEM_IUPAC_NAME']);
											echo "</code>\n</td>\n";
											echo "<td style='border-bottom: 1px dashed black;'>";
											/*if ($show_2D == 1) {*/ show_2d_smiles_structure($data['PUBCHEM_OPENEYE_ISO_SMILES']); //}
											echo "</td>";
											echo "</tr>";
										}
									} else {
										echo "\n<td>No Name Results</td></tr>\n";
									}
								}
							} else {
								echo "\nUH-OH!\n";
							}
							?>
						</td></tr>
						<tr><td colspan="3" align="center">
							<small>

								<?php 
							// result page navigation
							printf('<a href="%s?page=%d&2D=%s&query=%s">&lt;&lt;[PREV]</a>', 	$_PHP['SELF'], $prev, $show_2D, $searchString);
							echo '&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;';
							printf('<a href="%s?page=%d&2D=%s&query=%s">[NEXT]&gt;&gt;</a>', $_PHP['SELF'], $next, $show_2D, $searchString);
							?>

						</small><br/>
						<?php
					//-------------------
					//stop_time();
					$time = microtime(); 
					$time = explode(" ", $time); 
					$time = $time[1] + $time[0]; 
					$time2 = $time;
					$totaltime = ($time2 - $time1); 
					printf("<small>Query took %f sec.</small>", $totaltime);
					//-------------------
					?>
				</td></tr>
			</table> <!-- end results -->
		</blockquote>
	</div> <!-- end inner-frame -->
</div> <!-- end content -->
</form>
<!-- Allow your ticker to let you move quicker, by looking cool in the pool. -->
<?php include('footer.php'); ?>
